package com.zdkj.subway.check.controller.subwayRat.TVMRevenueVarianceAudit.TvmChaYiDXC;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zdkj.common.core.util.R;
import com.zdkj.common.log.annotation.SysLog;
import com.zdkj.common.security.service.PigxUser;
import com.zdkj.common.security.util.SecurityUtils;
import com.zdkj.subway.check.controller.CommonController;
import com.zdkj.subway.check.entity.*;
import com.zdkj.subway.check.enums.ResultDetailMsg;
import com.zdkj.subway.check.mapper.*;
import com.zdkj.subway.check.result.Result;
import com.zdkj.subway.check.service.FareDifferencesInAuditService;
import com.zdkj.subway.check.service.impl.subwayRat.TVMRevenueVarianceAudit.TvmChaYiDXC.TvmChaYiDXCService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * tvm层级差异待协查
 */
@RestController
@RequestMapping("/check")
@Slf4j
public class TvmChaYiDXCController {

    @Resource
    private TvmChaYiDXCService tvmChaYiDXCService;
    @Resource
    private SwAuTvmMoneyAssistMapper swAuTvmMoneyAssistMapper;
    @Resource
    private SwAuTvmDailyBillAssistMapper swAuTvmDailyBillAssistMapper;
    @Resource
    private SwAuTvmDailyBillAssistDetailMapper swAuTvmDailyBillAssistDetailMapper;
    @Resource
    private SwAuTvmSingleAssistMapper swAuTvmSingleAssistMapper;
    @Resource
    private SwStTvmDailyBillCashPayMapper swStTvmDailyBillCashPayMapper;
    @Resource
    private SwStTvmDailyBillElectronicPayMapper swStTvmDailyBillElectronicPayMapper;
    @Resource
    private SwAuTvmAmountDefferenceMapper swAuTvmAmountDefferenceMapper;
    @Resource
    private SwAuTvmBoxAmountDifferenceMapper swAuTvmBoxAmountDifferenceMapper;
    @Resource
    private SwAuItvmOnlineAmountHierarchyDifferenceMapper swAuItvmOnlineAmountHierarchyDifferenceMapper;
    @Autowired
    private CommonController commonController;
    @Resource
    SwAuTvmCashAmountHierarchyDifferenceMapper swAuTvmCashAmountHierarchyDifferenceMapper;
    @Autowired
    private FareDifferencesInAuditService fareDifferencesInAuditService;
    /**
     * 票款差异待协查
     * @param page
     * @param param
     * @return
     */
    @RequestMapping("/getTvmTicketCYXCList")
    @ApiOperation(value = "票款差异待协查", notes = "票款差异待协查")
    public Page getTvmTicketCYXCList(Page page, @RequestParam Map<String,String> param){
        List<Map<String,Object>> res=tvmChaYiDXCService.getTvmTicketCYXCList(param);
        page.setData(res,true);
        return page;
    }

    /**
     * 票款差异协查页面下发功能
     * @param param
     * @return
     */
    @RequestMapping("/sendStatusTvmTicketCYXC")
    @ApiOperation(value = "票款差异协查页面下发功能", notes = "票款差异协查页面下发功能")
    @Transactional(rollbackFor = Exception.class)
    public Result sendStatusTvmTicketCYXC(@RequestBody Map<String,Object> param){
        PigxUser user = SecurityUtils.getUser();
        String ids = String.valueOf(param.get("ids"));
        Result res=new Result(ResultDetailMsg.OK);
        try {
            for (String id : ids.split(",")) {
                //根据id查询
                SwAuTvmMoneyAssist swAuTvmMoneyAssist = swAuTvmMoneyAssistMapper.selectById(Integer.parseInt(id));
                //协查状态
                swAuTvmMoneyAssist.setAssistInvestStatus("2");
                //客运协查状态
                swAuTvmMoneyAssist.setKyAssistInvestStatus("2");
                //机电回馈状态
                swAuTvmMoneyAssist.setJdReplyStatus("0");
                //客运回馈状态
                swAuTvmMoneyAssist.setKyReplyStatus("0");
                swAuTvmMoneyAssist.setSendTime(new Date());
                //下发人工号
                swAuTvmMoneyAssist.setSendPersonCode(user.getUserCode());
                //下发人
                swAuTvmMoneyAssist.setSendPersonName(user.getNikeName());
                swAuTvmMoneyAssistMapper.updateById(swAuTvmMoneyAssist);
            }
            //走机电
            if(param.get("jdType")!=null){
                param.put("type",param.get("jdType").toString());
                commonController.innerBatch(param);
            }
            //走客运
            if(param.get("kyType")!=null){
                param.put("type",param.get("kyType").toString());
                commonController.innerBatch(param);
            }
        }catch (Exception e){
            log.error("{}",e);
            res.setResult(ResultDetailMsg.CHECK_UPDATE_ERROR);
            res.setMsg("数据下发失败！");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
       return res;
    }

    /**
     * TVM单台设备票款差异协查
     * @param page
     * @param swAuTvmMoneyAssist
     * @return
     */
    @RequestMapping("/getSwAuTvmSingleAssistList")
    @ApiOperation(value = "TVM单台设备票款差异协查", notes = "TVM单台设备票款差异协查")
    public Page getSwAuTvmSingleAssistList(Page page, SwAuTvmSingleAssist swAuTvmMoneyAssist){
        //区分查的是车站还是审核
        swAuTvmMoneyAssist.setIsFlag("check");
        List<Map<String,Object>> res=tvmChaYiDXCService.getSwAuTvmSingleAssistList(swAuTvmMoneyAssist);
        page.setData(res,true);
        return page;
    }

    /**
     * TVM单台设备下发协查
     * @param param
     * @return
     */
    @RequestMapping("/sendStatusSwAuTvmSingleAssist")
    @ApiOperation(value = "TVM单台设备下发协查", notes = "TVM单台设备下发协查")
    @Transactional(rollbackFor = Exception.class)
    public Result sendStatusSwAuTvmSingleAssist(@RequestBody Map<String,Object> param){
        PigxUser user = SecurityUtils.getUser();
        String ids = String.valueOf(param.get("ids"));
        Result res=new Result(ResultDetailMsg.OK);
        try {
            for (String id : ids.split(",")) {
                SwAuTvmSingleAssist swAuTvmSingleAssist = swAuTvmSingleAssistMapper.selectById(Integer.parseInt(id));
                //机电回馈状态
                swAuTvmSingleAssist.setReplyStatus("0");
                //客运回馈状态
                swAuTvmSingleAssist.setKyReplyStatus("0");
                //协查状态
                swAuTvmSingleAssist.setAssistInvestStatus("2");
                //客运协查状态
                swAuTvmSingleAssist.setKyAssistInvestStatus("2");
                SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
                String temp = sf.format(new Date());
                swAuTvmSingleAssist.setSendTime(temp);
                //下发人工号
                swAuTvmSingleAssist.setSendPersonCode(user.getUserCode());
                //下发人
                swAuTvmSingleAssist.setSendPersonName(user.getNikeName());
                swAuTvmSingleAssistMapper.updateById(swAuTvmSingleAssist);
            }
            //走机电
            if(param.get("jdType")!=null){
                param.put("type",param.get("jdType").toString());
                commonController.innerBatch(param);
            }
            //走客运
            if(param.get("kyType")!=null){
                param.put("type",param.get("kyType").toString());
                commonController.innerBatch(param);
            }
        }catch (Exception e){
            log.error("{}",e);
            res.setResult(ResultDetailMsg.CHECK_UPDATE_ERROR);
            res.setMsg("数据下发失败！");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return res;
    }


    /**
     * 批量删除单台设备未下发的
     * @param idList
     * @return
     */
    @SysLog("通过id批量删除单台设备未下发的")
    @DeleteMapping("/batchSingleDel")
    @ApiOperation(value = "通过id批量删除单台设备未下发的", notes = "通过id批量删除单台设备未下发的")
    @Transactional(rollbackFor = Exception.class)
    public R batchRemoveSingleById(@RequestBody List<Integer> idList) {
        List<SwAuTvmSingleAssist> list = swAuTvmSingleAssistMapper.selectList(new QueryWrapper<SwAuTvmSingleAssist>().lambda().in(SwAuTvmSingleAssist::getId, idList));
        //删除之前需要改变审核表的加入协查状态为未加入 '0'
        List<Integer> integerList = list.stream().map(SwAuTvmSingleAssist::getRelationid).collect(Collectors.toList());
        FareDifferencesInAudit fareDifferencesInAudit = new FareDifferencesInAudit();
        fareDifferencesInAudit.setIsAddSinglerAssist("0");
        UpdateWrapper<FareDifferencesInAudit> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id",integerList);
        fareDifferencesInAuditService.update(fareDifferencesInAudit,updateWrapper);
        //执行删除
        return R.ok(swAuTvmSingleAssistMapper.deleteBatchIds(idList));
    }
    /**
     * 批量删除分钱箱未下发的
     * @param idList
     * @return
     */
    @SysLog("通过id批量删除分钱箱未下发的")
    @DeleteMapping("/batchCashBoxDel")
    @ApiOperation(value = "通过id批量删除分钱箱未下发的", notes = "通过id批量删除分钱箱未下发的")
    @Transactional(rollbackFor = Exception.class)
    public R batchRemoveCashBoxById(@RequestBody List<Integer> idList) {
        List<SwAuTvmMoneyAssist> list = swAuTvmMoneyAssistMapper.selectList(new QueryWrapper<SwAuTvmMoneyAssist>().lambda().in(SwAuTvmMoneyAssist::getId, idList));
        //删除之前需要改变审核表的加入协查状态为未加入 '0'
        List<Integer> integerList = list.stream().map(SwAuTvmMoneyAssist::getRelationid).collect(Collectors.toList());
        SwAuTvmBoxAmountDifference swAuTvmBoxAmountDifference = new SwAuTvmBoxAmountDifference();
        swAuTvmBoxAmountDifference.setIsAddAssist("0");
        UpdateWrapper<SwAuTvmBoxAmountDifference> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id",integerList);
        swAuTvmBoxAmountDifferenceMapper.update(swAuTvmBoxAmountDifference,updateWrapper);
        //执行删除
        return R.ok(swAuTvmMoneyAssistMapper.deleteBatchIds(idList));
    }

    /**
     *TVM单台设备票款差异协查--批量一键替换功能修改
     * @param
     * @param
     * @return
     */
    @RequestMapping("/setSwAuTvmSingleAssist")
    @ApiOperation(value = "TVM单台设备票款差异协查--批量一键替换功能修改", notes = "TVM单台设备票款差异协查--批量一键替换功能修改")
    public Result setSwAuTvmSingleAssist(String ids){
        Result res=new Result(ResultDetailMsg.OK);
        try {
            for(String id:ids.split(",")) {
                SwAuTvmSingleAssist swAuTvmSingleAssist = swAuTvmSingleAssistMapper.selectById(Integer.parseInt(id));
                SwAuTvmAmountDefference swAuTvmAmountDefference = swAuTvmAmountDefferenceMapper.selectById(swAuTvmSingleAssist.getRelationid());//
                if (swAuTvmAmountDefference.getAuditStatus().equals("1")) {
                    res.setResult(ResultDetailMsg.CHECK_UPDATE_ERROR);
                    res.setMsg("收入已审核，不能再做一键替换");
                    return res;
                }

                //单台设备系统金额 = 回馈的差异金额 + 实点金额 - 补币金额 + 手工清出金额
//            BigDecimal singleDeviceSysMoney = swAuTvmSingleAssist.getReplyDiffMoney().add(swAuTvmAmountDefference.getActualMoney()).subtract(swAuTvmAmountDefference.getSupplementMoney())
//                    .add(swAuTvmAmountDefference.getHandOutMoney());//单台设备协查金额


                //TVM收入审核表增加 单台设备回馈的售卖 充值 退款 字段

                //系统金额  =  回馈的售卖 + 充值 + 退款
                BigDecimal singleDeviceSysMoney = swAuTvmSingleAssist.getReplySaleMoney().add(swAuTvmSingleAssist.getReplyRechargeMoney()).add(swAuTvmSingleAssist.getReplyErrorReturnMoney());//协查系统金额
                //TODO  更新单台设备的3个值 回馈的售卖 + 充值 + 退款
                swAuTvmAmountDefference.setSingleCashSellTicket(swAuTvmSingleAssist.getReplySaleMoney());
                swAuTvmAmountDefference.setSingleCashRecharge(swAuTvmSingleAssist.getReplyRechargeMoney());
                swAuTvmAmountDefference.setSingleCashReturn(swAuTvmSingleAssist.getReplyErrorReturnMoney());
                swAuTvmAmountDefference.setSingleDeviceSysMoney(singleDeviceSysMoney);
                //差异金额 = 实点金额 - 系统金额
                swAuTvmAmountDefference.setSingleDeviceDiffMoney(swAuTvmSingleAssist.getReplyDiffMoney());
                if (swAuTvmAmountDefference.getDataSource().equals("5")) {
                    swAuTvmAmountDefference.setSystemMoney(singleDeviceSysMoney);
                    //去掉手工清除，补币 BigDecimal diff = singleDeviceSysMoney.subtract(swAuTvmAmountDefference.getActualMoney()).subtract(swAuTvmAmountDefference.getHandOutMoney()).add(swAuTvmAmountDefference.getSupplementMoney());
                    //实点-系统
                    BigDecimal diff = swAuTvmAmountDefference.getActualMoney().subtract(singleDeviceSysMoney);
                    swAuTvmAmountDefference.setDiffMoney(diff);
                }
                swAuTvmAmountDefferenceMapper.updateById(swAuTvmAmountDefference);

                swAuTvmSingleAssist.setReplaceStatus("1");
                swAuTvmSingleAssistMapper.updateById(swAuTvmSingleAssist);
            }
        }catch (Exception e){
            log.error("{}=》{}","TVM单台设备票款差异协查--修改失败",e);
            res.appendMsg("TVM单台设备票款差异协查--修改失败");
            res.setResult(ResultDetailMsg.CHECK_UPDATE_ERROR);
        }
        return res;
    }


//    /**
//     *TVM单台设备票款差异协查--一键替换功能修改
//     * @param page
//     * @param swAuTvmSingleAssist
//     * @return
//     */
//    @RequestMapping("setSwAuTvmSingleAssist")
//    public Result setSwAuTvmSingleAssist(Page page,String masterId, SwAuTvmSingleAssist swAuTvmSingleAssist){
//        UpdateWrapper<SwAuTvmSingleAssist> updateWrapper=new UpdateWrapper<>();
//        Result res=new Result(ResultDetailMsg.OK);
//        try {
//            SwAuTvmAmountDefference swAuTvmAmountDefference = swAuTvmAmountDefferenceMapper.selectById(swAuTvmSingleAssist.getRelationid());//
//            if(swAuTvmAmountDefference.getAuditStatus().equals("1")){
//                res.setResult(ResultDetailMsg.CHECK_UPDATE_ERROR);
//                res.setMsg("收入已审核，不能再做一键替换");
//                return res;
//            }
//
//            //单台设备系统金额 = 回馈的差异金额 + 实点金额 - 补币金额 + 手工清出金额
////            BigDecimal singleDeviceSysMoney = swAuTvmSingleAssist.getReplyDiffMoney().add(swAuTvmAmountDefference.getActualMoney()).subtract(swAuTvmAmountDefference.getSupplementMoney())
////                    .add(swAuTvmAmountDefference.getHandOutMoney());//单台设备协查金额
//
//
//            //TVM收入审核表增加 单台设备回馈的售卖 充值 退款 字段
//
//            //系统金额  =  回馈的售卖 + 充值 + 退款
//            BigDecimal singleDeviceSysMoney = swAuTvmSingleAssist.getReplySaleMoney().add(swAuTvmSingleAssist.getReplyRechargeMoney()).add(swAuTvmSingleAssist.getReplyErrorReturnMoney());//协查系统金额
//            //TODO  更新单台设备的3个值 回馈的售卖 + 充值 + 退款
//            swAuTvmAmountDefference.setSingleCashSellTicket(swAuTvmSingleAssist.getReplySaleMoney());
//            swAuTvmAmountDefference.setSingleCashRecharge(swAuTvmSingleAssist.getReplyRechargeMoney());
//            swAuTvmAmountDefference.setSingleCashReturn(swAuTvmSingleAssist.getReplyErrorReturnMoney());
//            swAuTvmAmountDefference.setSingleDeviceSysMoney(singleDeviceSysMoney);
//            //差异金额 = 实点金额 - 系统金额
//            swAuTvmAmountDefference.setSingleDeviceDiffMoney(swAuTvmSingleAssist.getReplyDiffMoney());
//            if(swAuTvmAmountDefference.getDataSource().equals("5")){
//                swAuTvmAmountDefference.setSystemMoney(singleDeviceSysMoney);
//                //去掉手工清除，补币 BigDecimal diff = singleDeviceSysMoney.subtract(swAuTvmAmountDefference.getActualMoney()).subtract(swAuTvmAmountDefference.getHandOutMoney()).add(swAuTvmAmountDefference.getSupplementMoney());
//                BigDecimal diff = swAuTvmAmountDefference.getActualMoney().subtract(singleDeviceSysMoney);//实点-系统
//                swAuTvmAmountDefference.setDiffMoney(diff);
//            }
//            swAuTvmAmountDefferenceMapper.updateById(swAuTvmAmountDefference);
//
//            swAuTvmSingleAssist.setReplaceStatus("1");
//            updateWrapper.eq("id",masterId);
//            int update = swAuTvmSingleAssistMapper.update(swAuTvmSingleAssist, updateWrapper);
//        }catch (Exception e){
//            log.error("{}=》{}","TVM单台设备票款差异协查--修改失败",e);
//            res.appendMsg("TVM单台设备票款差异协查--修改失败");
//            res.setResult(ResultDetailMsg.CHECK_UPDATE_ERROR);
//        }
//        return res;
//    }
    /**
     * 结账单据协查
     * @param page
     * @param swAuTvmMoneyAssist
     * @return
     */
    @RequestMapping("/getSwAuTvmDailyBillAssistList")
    @ApiOperation(value = "结账单据协查", notes = "结账单据协查")
    public Page getSwAuTvmDailyBillAssistList(Page page, SwAuTvmDailyBillAssist swAuTvmMoneyAssist){
        List<Map<String,Object>> res=tvmChaYiDXCService.getSwAuTvmDailyBillAssistList(swAuTvmMoneyAssist);
        page.setData(res,true);
        return page;
    }


/**
 * 修改结帐单数据
 * @param ids
 * @return
 */
    @RequestMapping("/setSwAuTvmDailyBillAssistDetail")
    @ApiOperation(value = "修改结帐单数据", notes = "修改结帐单数据")
    public Result setSwAuTvmDailyBillAssistDetail(String ids/*,SwAuTvmDailyBillAssist swAuTvmDailyBillAssist, SwAuTvmDailyBillAssistDetail swAuTvmDailyBillAssistDetail*/){
        Result result=new Result(ResultDetailMsg.OK);//
        try {
            for(String id:ids.split(",")){
                //跟据id查询
                SwAuTvmDailyBillAssist swAuTvmDailyBillAssist = swAuTvmDailyBillAssistMapper.selectById(Integer.parseInt(id));
                Map<String,Object> map=new HashMap<>();
                map.put("relationid", swAuTvmDailyBillAssist.getId());
//                List<SwAuTvmDailyBillAssistDetail> swAuTvmDailyBillAssistDetails = swAuTvmDailyBillAssistDetailMapper.selectByMap(map);
                List<SwAuTvmDailyBillAssistDetail> swAuTvmDailyBillAssistDetails=swAuTvmDailyBillAssistDetailMapper.getAmountWithRelationid(map);
                //获取一条明细
                SwAuTvmDailyBillAssistDetail swAuTvmDailyBillAssistDetail=swAuTvmDailyBillAssistDetails.get(0);
//////////////////////////////////////////票款差异的 ↓////////////////////////////////
                //票款差异审核主表
            SwAuTvmAmountDefference swAuTvmAmountDefference = swAuTvmAmountDefferenceMapper.selectById(swAuTvmDailyBillAssist.getRelationid());
            if(swAuTvmAmountDefference.getAuditStatus().equals("1")){
                result.setResult(ResultDetailMsg.CHECK_UPDATE_ERROR);
                result.setMsg("收入已审核，不能再做一键替换");
                return result;
            }
                BigDecimal tvmBillOrderMoney = swAuTvmDailyBillAssistDetail.getRSellTicketMoney().
                        add(swAuTvmDailyBillAssistDetail.getRChangeTotalMoney()).
                        add(swAuTvmDailyBillAssistDetail.getRReturnTotal());//结账单据金额替换
                //结账单据金额替换
                swAuTvmAmountDefference.setTvmBillOrderMoney(tvmBillOrderMoney);
                swAuTvmAmountDefference.setTvmBillReceivePapaer(swAuTvmDailyBillAssistDetail.getRReceiveNotes());
                swAuTvmAmountDefference.setTvmBillReceiveCoins(swAuTvmDailyBillAssistDetail.getRReceiveCoin());
                swAuTvmAmountDefference.setTvmBillNoteChange(swAuTvmDailyBillAssistDetail.getRNoteChange());
                swAuTvmAmountDefference.setTvmBillCoinChange(swAuTvmDailyBillAssistDetail.getRCoinChange());
                swAuTvmAmountDefference.setTvmBillCashSellTicket(swAuTvmDailyBillAssistDetail.getRSellTicketMoney());
                swAuTvmAmountDefference.setTvmBillCashRecharge(swAuTvmDailyBillAssistDetail.getRRechargeTotal());
                swAuTvmAmountDefference.setTvmBillCashReturn(swAuTvmDailyBillAssistDetail.getRReturnTotal());
                swAuTvmAmountDefference.setTvmBillElecSellTicket(swAuTvmDailyBillAssistDetail.getRESellTicketMoney());
                swAuTvmAmountDefference.setTvmBillElecRecharge(swAuTvmDailyBillAssistDetail.getRERechargeTotal());
                swAuTvmAmountDefference.setTvmBillElecReturn(swAuTvmDailyBillAssistDetail.getREReturnTotal());

                //为1时同时替换系统金额和差异
            if(swAuTvmAmountDefference.getDataSource().equals("1")){
                swAuTvmAmountDefference.setSystemMoney(tvmBillOrderMoney);
                //去掉手工清除，补币的BigDecimal diff = tvmBillOrderMoney.subtract(swAuTvmAmountDefference.getActualMoney()).subtract(swAuTvmAmountDefference.getHandOutMoney()).add(swAuTvmAmountDefference.getSupplementMoney());
                //BigDecimal diff =swAuTvmAmountDefference.getActualMoney().subtract(tvmBillOrderMoney);
				 BigDecimal diff =swAuTvmAmountDefference.getActualMoney().subtract(tvmBillOrderMoney);
                swAuTvmAmountDefference.setDiffMoney(diff);
            }
            swAuTvmAmountDefferenceMapper.updateById(swAuTvmAmountDefference);
/////////////////////////////////////票款差异分钱箱的表更改 ↓ //////////////////////
           // relationid
            Map<String,Object> param=new HashMap<>();
            param.put("relationid",swAuTvmAmountDefference.getId());
            List<SwAuTvmBoxAmountDifference> swAuTvmBoxAmountDifferences = swAuTvmBoxAmountDifferenceMapper.selectByMap(param);
            for(SwAuTvmBoxAmountDifference swAuTvmBoxAmountDifference:swAuTvmBoxAmountDifferences){
                //swAuTvmBoxAmountDifference 获取票款分钱箱的数据
                //结账单据金额替换
                swAuTvmBoxAmountDifference.setBillOrderMoney(tvmBillOrderMoney);
                //为1时同时替换系统金额和差异
                if(swAuTvmBoxAmountDifference.getDataSource().equals("1")){
				    String cashBoxType = swAuTvmBoxAmountDifference.getCashBoxType();
                    //硬币回收箱
					if("1".equals(cashBoxType)){
					      tvmBillOrderMoney = swAuTvmDailyBillAssistDetail.getRReceiveCoin();

					}
                    //硬币找零箱
					else if("2".equals(cashBoxType)){
					      tvmBillOrderMoney = swAuTvmBoxAmountDifference.getSupplementMoney().subtract(swAuTvmDailyBillAssistDetail.getRCoinChange());
					
					}
                    //纸币回收箱
					else if("3".equals(cashBoxType)){
                          tvmBillOrderMoney = swAuTvmDailyBillAssistDetail.getRReceiveNotes();
					  
					}
                    //纸币找零箱
					else if("4".equals(cashBoxType)){
					     tvmBillOrderMoney = swAuTvmBoxAmountDifference.getSupplementMoney().subtract(swAuTvmDailyBillAssistDetail.getRNoteChange());
					}
                    swAuTvmBoxAmountDifference.setSystemMoney(tvmBillOrderMoney);
                    //BigDecimal diff = tvmBillOrderMoney.subtract(swAuTvmBoxAmountDifference.getActualMoney()).subtract(swAuTvmBoxAmountDifference.getHandOutMoney()).add(swAuTvmBoxAmountDifference.getSupplementMoney());
                    BigDecimal diff =swAuTvmAmountDefference.getActualMoney().subtract(tvmBillOrderMoney);
                    swAuTvmBoxAmountDifference.setDiffMoney(diff);
                }
                swAuTvmBoxAmountDifferenceMapper.updateById(swAuTvmBoxAmountDifference);
            }
 //////////////////////////////////////////  ///////////票款差异-现金层级差异/////////////////////////////// //////////////////////////////////////////
                Map<String,Object> param3=new HashMap<>();
                param3.put("relation_source_id",swAuTvmAmountDefference.getId());
                List<SwAuTvmCashAmountHierarchyDifference> swAuTvmCashAmountHierarchyDifferences = swAuTvmCashAmountHierarchyDifferenceMapper.selectByMap(param3);
                for(SwAuTvmCashAmountHierarchyDifference s:swAuTvmCashAmountHierarchyDifferences){
                    s.setDepositAmount(swAuTvmDailyBillAssistDetail.getRRechargeTotal());
                    s.setSaleAmount(swAuTvmDailyBillAssistDetail.getRSellTicketMoney());
                    s.setErrorRefundAmount(swAuTvmDailyBillAssistDetail.getRReturnTotal());
                    swAuTvmCashAmountHierarchyDifferenceMapper.updateById(s);
                }

////////////////////////////////////////// itvm票款差异协查-层级差异信息表的 /////////////////////////////
//            RELATION_SOURCE_ID
            Map<String,Object> param2=new HashMap<>();
            param2.put("relation_source_id",swAuTvmAmountDefference.getId());
            List<SwAuItvmOnlineAmountHierarchyDifference> swAuItvmOnlineAmountHierarchyDifferences = swAuItvmOnlineAmountHierarchyDifferenceMapper.selectByMap(param2);
            for(SwAuItvmOnlineAmountHierarchyDifference swAuItvmOnlineAmountHierarchyDifference:swAuItvmOnlineAmountHierarchyDifferences){
                swAuItvmOnlineAmountHierarchyDifference.setDepositAmount(swAuTvmDailyBillAssistDetail.getRRechargeTotal());
                swAuItvmOnlineAmountHierarchyDifference.setSaleAmount(swAuTvmDailyBillAssistDetail.getRSellTicketMoney());
                swAuItvmOnlineAmountHierarchyDifference.setErrorRefundAmount(swAuTvmDailyBillAssistDetail.getRReturnTotal());
                swAuItvmOnlineAmountHierarchyDifferenceMapper.updateById(swAuItvmOnlineAmountHierarchyDifference);
            }

            UpdateWrapper<SwAuTvmDailyBillAssist> s=new UpdateWrapper<>();
            //现金总计
//            swAuTvmDailyBillAssist.setCashTotal(swAuTvmDailyBillAssistDetail.getRRechargeTotal().add(swAuTvmDailyBillAssistDetail.getRSellTicketMoney()).subtract(swAuTvmDailyBillAssistDetail.getRReturnTotal()));
            //电子总计
//            swAuTvmDailyBillAssist.setElecTotal(swAuTvmDailyBillAssistDetail.getRERechargeTotal().add(swAuTvmDailyBillAssistDetail.getRESellTicketMoney()).subtract(swAuTvmDailyBillAssistDetail.getREReturnTotal()));
                //改为哦已替换
                swAuTvmDailyBillAssist.setReplaceStatus("1");
            s.eq("id",id);
            Integer res2=swAuTvmDailyBillAssistMapper.update(swAuTvmDailyBillAssist,s);
        }
            return result;
        }catch (Exception e){
            log.error("{}",e);
            result.setResult(ResultDetailMsg.CHECK_SELECT_ERROR);
            result.appendMsg("修改结账明细表出错！");
        }
        return result;
    }
    /**
     * 获取结账单详情信息
     * @param page
     * @param swAuTvmMoneyAssist
     * @return
     */
    @RequestMapping("/getSwAuTvmDailyBillAssistDetail")
    @ApiOperation(value = "获取结账单详情信息", notes = "获取结账单详情信息")
    public Result getSwAuTvmDailyBillAssistDetail(Page page, SwAuTvmDailyBillAssistDetail swAuTvmMoneyAssist){
        Result result=new Result(ResultDetailMsg.OK);
        try {
            List<Map<String,Object>> res=tvmChaYiDXCService.getSwAuTvmDailyBillAssistDetail(swAuTvmMoneyAssist);
            result.setData(res);
        }catch (Exception e){
            log.error("{}",e);
            result.setResult(ResultDetailMsg.CHECK_SELECT_ERROR);
            result.appendMsg("查询结账明细表出错！");
        }
        return result;
    }


    /**
     * 结账单据下发
     * @param param
     * @return
     */
    @RequestMapping("/sendStatusSwAuTvmDailyBillAssist")
    @ApiOperation(value = "结账单据下发", notes = "结账单据下发")
    @Transactional(rollbackFor = Exception.class)
    public Result sendStatusSwAuTvmDailyBillAssist(@RequestBody Map<String,Object> param){
        String ids = String.valueOf(param.get("ids"));
        Result result=new Result(ResultDetailMsg.OK);
        try {
            for (String id : ids.split(",")) {
                SwAuTvmDailyBillAssist swAuTvmDailyBillAssist = swAuTvmDailyBillAssistMapper.selectById(Integer.parseInt(id));
                swAuTvmDailyBillAssist.setAssistInvestStatus("2");
                swAuTvmDailyBillAssist.setSendTime(getNowTime("seconds"));
                swAuTvmDailyBillAssistMapper.updateById(swAuTvmDailyBillAssist);
            }
            //根据不同类型发代办
            commonController.innerBatch(param);
        }catch (Exception e){
            log.error("{}",e);
            result.setResult(ResultDetailMsg.CHECK_UPDATE_ERROR);
            result.setMsg("数据下发失败！");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return result;
    }

    /**
     * 根据不同类型获取不同格式时间
     * @param type
     * @return
     */
    private String getNowTime(String type){
        String format="yyyy-MM-dd";
        if(type.equals("day")){
            format="yyyy-MM-dd";
        }else if(type.equals("seconds")){
            format="yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sm=new SimpleDateFormat(format);
        String format1 = sm.format(new Date());
        return format1;
    }
    /**
     * TVM日结账单据录入
     * @param page
     * @param swStTvmDailyBill
     * @return
     */
    @RequestMapping("/getSwStTvmDailyBillList")
    @ApiOperation(value = "TVM日结账单据录入", notes = "TVM日结账单据录入")
    public Page getSwStTvmDailyBillList(Page page, SwStTvmDailyBill swStTvmDailyBill){
        List<Map<String,Object>> res=tvmChaYiDXCService.getSwStTvmDailyBillList(swStTvmDailyBill);
        page.setData(res,true);
        return page;
    }

    /**
     * TVM日结账单据录入-现金表
     * @param page
     * @param swStTvmDailyBillCashPay
     * @return
     */
    @RequestMapping("getSwStTvmDailyBillCashPayList")
    public Page getSwStTvmDailyBillCashPayList(Page page, SwStTvmDailyBillCashPay swStTvmDailyBillCashPay){
        Map<String,Object> param=new HashMap<>();
        param.put("relation_id",swStTvmDailyBillCashPay.getRelationId());
        List<SwStTvmDailyBillCashPay> res=swStTvmDailyBillCashPayMapper.selectByMap(param);
        page.setData(res,false);
        return page;
    }

    /**
     * TVM日结账单据录入-电子表
     * @param page
     * @param swStTvmDailyBillElectronicPay
     * @return
     */
    @RequestMapping("/getSwStTvmDailyBillElectronicPayList")
    @ApiOperation(value = "TVM日结账单据录入-电子表", notes = "TVM日结账单据录入-电子表")
    public Page getSwStTvmDailyBillElectronicPayList(Page page, SwStTvmDailyBillElectronicPay swStTvmDailyBillElectronicPay){
        Map<String,Object> param=new HashMap<>();
        param.put("relation_id",swStTvmDailyBillElectronicPay.getRelationId());
        List<SwStTvmDailyBillElectronicPay> res=swStTvmDailyBillElectronicPayMapper.selectByMap(param);
        page.setData(res,false);
        return page;
    }


    /**
     * 添加TVM日结账单据录入-现金，电子表
     * @param electrons
     * @param cashs
     * @param swStTvmDailyBill
     * @return
     */
    @RequestMapping("/addSwStTvmDailyBills")
    @ApiOperation(value = "添加TVM日结账单据录入-现金，电子表", notes = "添加TVM日结账单据录入-现金，电子表")
    public Result addSwStTvmDailyBills(String cashs,String electrons, SwStTvmDailyBill swStTvmDailyBill) throws IllegalAccessException, InstantiationException {
        Result result=new Result(ResultDetailMsg.OK);
        try {
            Integer res=tvmChaYiDXCService.addSwStTvmDailyBills(cashs,electrons,swStTvmDailyBill);
        }catch (Exception e){
            log.error("{}=>{}","添加结账单数据失败",e);
            result.setResult(ResultDetailMsg.CHECK_UPDATE_ERROR);
            result.appendMsg("添加结账单数据失败");
        }
        return result;
    }
}
